package model.action.order_deal_action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import model.dap.dao.FavoriteDAO;
import model.dap.domain.Consumer;
import model.dap.domain.Deal;
import model.dap.mapper.FavoriteMapper;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import common.base.BaseAction;

@Results({
		@Result(name = "success", type = "tiles", location = "tiles.FavoritePage"),
		@Result(name = "dealdetailload", type = "tiles", location = "tiles.Viewdealdetail"),
		@Result(name = "ERROR", type = "tiles", location = "tiles.error") })
@ParentPackage("master_login")
public class FavoriteAction extends BaseAction {

	protected static Logger log = Logger.getLogger(FavoriteAction.class);
	private static final long serialVersionUID = 1L;
	private static final String COUNT_STRING = "count_string";
	
	private String userID;

	private List<Deal> dealresults; // custApps=deal;

	private ArrayList<String> lstPage; //

	public List<Deal> getDealresults() {
		return dealresults;
	}

	public void setDealresults(List<Deal> dealresults) {
		this.dealresults = dealresults;
	}

	private String contextPath = null;
	private FavoriteMapper mapDeal = new FavoriteDAO(); // mapMastApp=mapDeal

	public String getContextPath() {
		return contextPath;
	}

	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}

	public String execute() {
		return SUCCESS;
	}

	public String display() {
		return NONE;
	}
	
	public String getUserID() {
		userID = ServletActionContext.getRequest().getParameter("userID");
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}
	private Consumer consumer = new Consumer();
	// ======================================================================================================
	// GET, SET PROPERTY

	public Consumer getConsumer() {
		return consumer;
	}

	public void setConsumer(Consumer consumer) {
		this.consumer = consumer;
	}
	@Action("/master/favorite-deal")
	public String load() {
		getCityList2();
		loadParentDealCategory();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		contextPath = request.getContextPath();
		
		consumer.setUserID(getCurrentUser().getUserID());
		String userID2 = getCurrentUser().getUserID();
		// Láº¥y sá»‘ trang

		try {
			if (request.getParameter("numberPage") != null) {
				this.setOffset(Integer.parseInt(request
						.getParameter("numberPage")));
			}
			
			dealresults = mapDeal.searchForFavoriteDeal(this.getItemPerPage(),
					((this.getOffset() - 1) * this.getItemPerPage()),userID2);
			for(int i=0; i<dealresults.size();i++){
				if(dealresults.get(i).getCurrencyID().equals("vnd")){
				dealresults.get(i).setNewSellMoney(newMoney(dealresults.get(i).getSellPrice()+""));
				dealresults.get(i).setNewRootMoney(newMoney(dealresults.get(i).getRootpPrice()+""));
				}
				else{
					dealresults.get(i).setNewSellMoney(dealresults.get(i).getSellPrice()+"");
					dealresults.get(i).setNewRootMoney(dealresults.get(i).getRootpPrice()+"");	
				}
			}
			this.setCount(mapDeal.countForFavorite(userID2));
			// Store the count
			session.setAttribute(COUNT_STRING, this.getCount());
			//
			lstPage = new ArrayList<String>();
			if (count % ItemPerPage == 0) {
				numberPage = count / ItemPerPage;
			} else {
				numberPage = count / ItemPerPage + 1;
			}
			totalPage = "" + numberPage;
			// tao vong lap so trang
			for (int i = 0; i < numberPage; i++) {
				lstPage.add("" + (i + 1));
			}

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public static String getCountString() {
		return COUNT_STRING;
	}

	public FavoriteMapper getMapDeal() {
		return mapDeal;
	}

	public void setMapDeal(FavoriteMapper mapDeal) {
		this.mapDeal = mapDeal;
	}

	public ArrayList<String> getLstPage() {
		return lstPage;
	}

	public void setLstPage(ArrayList<String> lstPage) {
		this.lstPage = lstPage;
	}

	/*@Action("/master/view-deal-detail")
	public String OrderDeal() {
		loadParentDealCategory();
		getCityList2();
		return "dealdetailload";
	}*/

	// @Action("/master/home")
	// public String home() {
	// return "success";
	// }
}
